#include "stdafx.h"
#include <pimstore.h>
#include "ListCategories.h"
#include "Settings.h"
#include "Language.h"
#include "Theme.h"
#include "PoomData.h"
#include "CommonWM.h"
#include "GraphicFunctions.h"

#ifdef _DEBUG
#include "crtdbg.h"
#endif

void CListItemCategory::Draw(HDC hdc, const RECT& rc) const
{
  RECT rItem = rc;
  // separator
  rItem.bottom = rItem.top + 1;
  FillRect(hdc, &rItem, Theme::hbrDetailItemSeparator);

  rItem.top = rItem.bottom;
  rItem.bottom = rItem.top + this->height - 1;

  // background of selected item
  if (this->GetHovered())
  {
    GraphicFunctions::DrawGradientGDI(hdc, rItem, Theme::rgbDetailsHoverBackground1, Theme::rgbDetailsHoverBackground2);
  }

  PrepareDraw(hdc, rc);

  // Draw String
  SetTextAlign(hdc, TA_CENTER);
  rItem.left += (rItem.right - rItem.left) / 2;
  rItem.top += (height - Theme::CategoriesFontSize) / 2;
  if (this->GetHovered())
  {
    // shadow for selected item
    ::SetTextColor(hdc, Theme::rgbDetailsItemShadow);
    ExtTextOut(hdc, rItem.left, rItem.top - 1, 0, NULL, this->name, this->length, 0);
  }
  ::SetTextColor(hdc, Theme::rgbDetailsItemText);
  ExtTextOut(hdc, rItem.left, rItem.top, 0, NULL, this->name, this->length, 0);
}

void CListItemCategory::PrepareDraw(HDC hdc, const RECT& rc) const
{
  ::SelectObject(hdc, Theme::CategoriesFont);
}

bool CListItemCategory::OnMouseClick(HWND hParent, int x, int y)
{
  PostCommand(hParent, CMD_LIST_SET_CATEGORY);
  return true;
}

bool CListItemCategory::OnKeyUp(HWND hParent, int vKey)
{
  switch(vKey)
  {
  case VK_TACTION:
    PostCommand(hParent, CMD_LIST_SET_CATEGORY);
    return true;
  }
  return false;
}

bool CListItemCategory::OnLongTap(HWND hParent)
{
  PostCommand(hParent, CMD_LIST_SET_CATEGORY);
  return true;
}

void CListItemCategoryFav::PrepareDraw(HDC hdc, const RECT& rc) const
{
  ::SelectObject(hdc, Theme::CategoriesFontBold);
}

void CListItemCategoryBack::PrepareDraw(HDC hdc, const RECT& rc) const
{
  ::SelectObject(hdc, Theme::CategoriesFontBold);
}

bool CListItemCategoryBack::OnMouseClick(HWND hParent, int x, int y)
{
  PostCommand(hParent, CMD_LIST_HIDE_CATEGORIES);
  return true;
}

bool CListItemCategoryBack::OnKeyUp(HWND hParent, int vKey)
{
  switch(vKey)
  {
  case VK_TACTION:
    PostCommand(hParent, CMD_LIST_HIDE_CATEGORIES);
    return true;
  }
  return false;
}

bool CListItemCategoryBack::OnLongTap(HWND hParent)
{
  PostCommand(hParent, CMD_LIST_HIDE_CATEGORIES);
  return true;
}

CListCategories::CListCategories(HWND hWnd)
  : CList(hWnd, Theme::iCategoriesItemHeight)
{
  Populate();

  style.clrBackground1 = Theme::rgbDetailsBackground1;
  style.clrBackground2 = Theme::rgbDetailsBackground2;
  style.hbrBackground  = Theme::hbrDetailsBackground;
}

void CListCategories::Populate()
{
  HRESULT hr;
  IFolder* pFolder = NULL;
  // Get the contacts folder.
  hr = PoomData::GetApp()->GetDefaultFolder(olFolderContacts, &pFolder);
  if (FAILED(hr))
    return;

  IItem*  pFolderItem = NULL;
  // Get the IItem object representing a IFolder object.
  hr = pFolder->QueryInterface(IID_IItem, (LPVOID*)&pFolderItem);
  if (FAILED(hr))
  {
    pFolder->Release();
    return;
  }

  PropertyValues vals;
  if (!PoomData::GetItemProperty(pFolderItem, PIMPR_FOLDER_CATEGORIES, vals))
  {
    pFolderItem->Release();
    pFolder->Release();
    return;
  }

  // add Back action
  CListItemCategory* pItem = new CListItemCategoryBack();
  if (pItem)
  {
    pItem->SetName(Language::back);
    Add(pItem);
  }

  // Copy the list of categories for use outside of this function.
  size_t len = ::_tcslen(vals[0].val.lpwstr);
  if (len > 0)
  {
    // get number of categories
    int numCats = 0;
    for (size_t i = 0; i <= len; i++)
      if (vals[0].val.lpwstr[i] == TEXT(',') || vals[0].val.lpwstr[i] == 0)
        numCats++;

    if (numCats > 0)
    {
      TCHAR* lastCat = vals[0].val.lpwstr;
      for (size_t i = 0; i <= len; i++)
      {
        if (vals[0].val.lpwstr[i] == TEXT(',') || vals[0].val.lpwstr[i] == 0)
        {
          vals[0].val.lpwstr[i] = 0;
          // trim spaces
          while(_istspace(*lastCat) && lastCat < vals[0].val.lpwstr + i)
            lastCat++;
          // copy
          if (_tcsicmp(lastCat, Settings::favorite_category) == 0)
            pItem = new CListItemCategoryFav();
          else
            pItem = new CListItemCategory();
          if (pItem)
          {
            pItem->SetName(lastCat);
            Add(pItem);
          }

          // shift next
          lastCat = vals[0].val.lpwstr + i + 1;
        }
      }
    }
  }

  vals.Free();
  pFolderItem->Release();
  pFolder->Release();

  CalculateHeights();
}

void CListCategories::Draw(HDC hdc, HDC hdcTmp, const RECT& rClip)
{
  CList::Draw(hdc, hdcTmp, rClip);

  RECT rc = rect;
  rc.top = rc.bottom - 1;
  FillRect(hdc, &rc, Theme::hbrDetailItemSeparator);
}
